added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / VBSparseFile / MainModule.vb
blob02c15d7c87197b3df5469c542da9afdcba52fcdf
1 '****************************** Module Header ******************************'
2 ' Module Name: MainModule.vb
3 ' Project: VBSparseFile
4 ' Copyright (c) Microsoft Corporation.
5 '
6 ' VBSparseFile demonstrates the common operations on sparse files. A sparse
7 ' file is a type of computer file that attempts to use file system space more
8 ' efficiently when blocks allocated to the file are mostly empty. This is
9 ' achieved by writing brief information (metadata) representing the empty
10 ' blocks to disk instead of the actual "empty" space which makes up the
11 ' block, using less disk space. You can find in this example the creation of
12 ' sparse file, the detection of sparse attribute, the retrieval of sparse
13 ' file size, and the query of sparse file layout.
15 ' This source is subject to the Microsoft Public License.
16 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
17 ' All other rights reserved.
19 ' History:
20 ' * 7/7/2009 11:34 PM Jialiang Ge Created
21 '***************************************************************************'
23 #Region "Imports directives"
25 Imports System.IO
27 #End Region
30 Module MainModule
32 Sub Main()
34 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
35 ' Determine if the volume support sparse streams.
38 If Not SparseFile.VolumeSupportsSparseFiles("C:\") Then
39 Console.WriteLine("Volume {0} does not support sparse streams", _
40 "C:\")
41 Return
42 End If
45 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
46 ' Create a sparse file.
49 Dim fileName As String = "SparseFile.tmp"
50 Console.WriteLine("Create sparse file: {0}", fileName)
52 Using fs As FileStream = SparseFile.Create(fileName)
54 ' Write a large block of data
55 Const blockLength As Integer = 512 * 1024 ' 512KB
56 Dim block As Byte() = New Byte(blockLength - 1) {}
57 For i As Integer = 0 To blockLength - 1
58 block(i) = &HFF
59 Next i
61 fs.Write(block, 0, blockLength)
63 ' Set some sparse ranges in the block
65 SparseFile.SetSparseRange(fs.SafeFileHandle, 0, &H10000)
66 SparseFile.SetSparseRange(fs.SafeFileHandle, &H20000, &H20000)
68 ' Set sparse block at the end of the file
70 ' 1GB sparse zeros are extended to the end of the file
71 fs.SetLength(fs.Length + &H40000000)
73 End Using
76 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
77 ' Determine if a file is sparse.
80 Dim isSparse As Boolean = SparseFile.IsSparseFile(fileName)
81 Console.WriteLine("The file is{0} sparse", IIf(isSparse, "", " not"))
84 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
85 ' Get file size.
88 SparseFile.GetSparseFileSize(fileName)
91 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
92 ' Query the sparse file layout.
95 SparseFile.GetSparseRanges(fileName)
97 End Sub
99 End Module